package switchviz.graphic.signal;

import org.jdom.Element;

public class ComplexSignalPersistence extends BasicSignalPersistence
{
	private final static String SIGFIRSTALIAS = "firstalias";
	private final static String SIGSECONDALIAS = "secondalias";
	private final static String OFFSET = "offset";
	private final static String COEFFICIENT = "coefficient";
	private final static String LOGARITHM = "logarithm";
	private final static String OPERATION = "operation";
	
	private ComplexSignalDataModel _datamodel = null;
	
	public ComplexSignalPersistence(Element root) 
	{
		super(root);
	}

	public ComplexSignalDataModel getDataModelFromElement()
	{
		super.getDataModelFromElementBasic();
		if(_root.getChild(SIGFIRSTALIAS) != null)
			getDataModel().setFirstalias(_root.getChildText(SIGFIRSTALIAS));
		if(_root.getChild(SIGSECONDALIAS) != null)
			getDataModel().setSecondalias(_root.getChildText(SIGSECONDALIAS));
		if(_root.getChild(OFFSET) != null)
			getDataModel().setOffset(Float.parseFloat(_root.getChildText(OFFSET)));
		if(_root.getChild(COEFFICIENT) != null)
			getDataModel().setCoefficient(Float.parseFloat(_root.getChildText(COEFFICIENT)));
		if(_root.getChild(LOGARITHM) != null)
			getDataModel().setLogarithm(Boolean.parseBoolean(_root.getChildText(LOGARITHM)));
		if(_root.getChild(OPERATION) != null)
			getDataModel().setOperation(Integer.parseInt(_root.getChildText(OPERATION)));
		return getDataModel();
	}
	
	public Element setDataModel(ComplexSignalDataModel datamodel)
	{
		_datamodel = datamodel;
		return saveDataModel();
	}
	
	protected ComplexSignalDataModel getDataModel()
	{
		if (_datamodel == null) 
			_datamodel = new ComplexSignalDataModel();
		return _datamodel;
	}
	
	protected Element saveDataModel()
	{
		super.saveDataModelBasic();
		addNode(SIGFIRSTALIAS, getDataModel().getFirstalias());
		addNode(SIGSECONDALIAS, getDataModel().getSecondalias());
		addNode(OPERATION, String.valueOf(getDataModel().getOperation()));
		addNode(OFFSET, String.valueOf(getDataModel().getOffset()));
		addNode(COEFFICIENT, String.valueOf(getDataModel().getCoefficient()));
		addNode(LOGARITHM, String.valueOf(getDataModel().isLogarithm()));
		return _root;
	}
}
